package com.xb.sourest.base

import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.TextView
import com.xb.sourest.R
import com.xb.sourest.view.MultiStateView

/**
 * 作用：多状态View的操作代理类
 * 作者：unfind
 * 时间：2019年05月21日10:00:36
 */
class MultiStateViewOpera(var multiStateView: MultiStateView, var callBack: MultiStateCallBack) {
    private var loadingView: View?
    private var errorView: View?
    private var emptyView: View?
    private var errorInfoTextView:TextView
    private var emptyInfoTextView:TextView
    private var emptyImageView:ImageView
    private var errorImageView:ImageView
    private var syImageView:ImageView
    private var anim:ObjectAnimator
    init {
        loadingView = multiStateView.getView(MultiStateView.VIEW_STATE_LOADING)
        errorView = multiStateView.getView(MultiStateView.VIEW_STATE_ERROR)
        errorInfoTextView = errorView!!.findViewById(R.id.multi_state_error_hint)
        emptyView = multiStateView.getView(MultiStateView.VIEW_STATE_EMPTY)
        emptyInfoTextView = emptyView!!.findViewById(R.id.multi_state_empty_hint)
        syImageView = loadingView!!.findViewById(R.id.loading_sy)
        anim = ObjectAnimator.ofFloat(syImageView, "rotation", 0f,360f)
        anim.interpolator = LinearInterpolator()
        anim.repeatCount = -1
        anim.duration = 1000

        emptyImageView = emptyView!!.findViewById(R.id.multi_state_empty_image)
        emptyImageView.setOnClickListener{
            callBack.request()
        }
        errorImageView = errorView!!.findViewById(R.id.multi_state_error_image)
        errorImageView.setOnClickListener{
            callBack.request()
        }

    }

    // 加载
    fun loading() {
        multiStateView.viewState = MultiStateView.VIEW_STATE_LOADING
        if (!anim.isPaused) {
            anim.start()
        } else {
            anim.resume()
        }
    }

    // 设置错误状态
    fun error() {
        if (anim.isStarted) {
            anim.pause()
        }
        multiStateView.viewState = MultiStateView.VIEW_STATE_ERROR
    }
    // 设置错误状态
    fun error(str:String) {
        if (anim.isStarted) {
            anim.pause()
        }
        multiStateView.viewState = MultiStateView.VIEW_STATE_ERROR
        errorInfoTextView.text = str
    }
    // 设置错误状态
    fun error(resId:Int) {
        if (anim.isStarted) {
            anim.pause()
        }
        multiStateView.viewState = MultiStateView.VIEW_STATE_ERROR
        errorInfoTextView.setText(resId)
    }


    // 设置空数据装填
    fun empty() {
        if (anim.isStarted) {
            anim.pause()
        }
        multiStateView.viewState = MultiStateView.VIEW_STATE_EMPTY
    }
    // 设置空数据装填
    fun empty(str:String) {
        if (anim.isStarted) {
            anim.pause()
        }
        multiStateView.viewState = MultiStateView.VIEW_STATE_EMPTY
        emptyInfoTextView.text = str
    }
    // 设置空数据装填
    fun empty(resId:Int) {
        if (anim.isStarted) {
            anim.pause()
        }
        multiStateView.viewState = MultiStateView.VIEW_STATE_EMPTY
        emptyInfoTextView.setText(resId)
    }

    // 设置内容
    fun content() {
        if (anim.isStarted) {
            anim.pause()
        }
        multiStateView.viewState = MultiStateView.VIEW_STATE_CONTENT
    }

    interface MultiStateCallBack{
        fun request()
    }
}